import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Date;
import java.util.Calendar;


public class theInterface extends JFrame implements ActionListener
{
   // a JTextField for each number and one for the result
   private  JTextField number1JTextField = new JTextField(25);

   private final JTextField resultJTextField = new JTextField(25);
   public JLabel loginLabel = new JLabel("");
   public JButton computeJButton = new JButton("Login");
   public JButton reNew = new JButton("Another Driver?");
   public Container contents;
   public Container contents1;
   theInterface x;
  
   // Construct a new window 
   public theInterface()
   {  
      contents = getContentPane();
      setTitle("Holiday request");
      contents.setLayout(new GridLayout(0, 1));

      // Add  textfields on the contents
      contents.add(new JLabel("driver ID: "));
      contents.add(number1JTextField);
     
      // create a new button and add it to the contents
      contents.add(computeJButton);
      // add a ActionListener for this button
      computeJButton.addActionListener(this);
      // add in the contents a textlabel 
      contents.add(loginLabel);
      // display the result in the restultextfield
      //  contents.add(resultJTextField);
      // exit the program in case the window it's closed
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      pack();
   } //cont
   
 
   private JLabel statusReq;
   private JLabel holidayStatus;
   private final JTextField startDate = new JTextField(25);
   private final JTextField endDate = new JTextField(25);
   JButton reqHoliday = new JButton("Request Holiday");
   //driverLogedIn
   int driverId;
   private static theInterface log;
   //construct an holiday request interface

   public theInterface(int driverID)
   {
      contents1 = getContentPane();
      setTitle("Holiday_request");
      driverId=driverID;
      contents1.setLayout(new GridLayout(0, 1));
      JPanel contents2 = new JPanel(new GridLayout(0, 2));
      JPanel contents3 = new JPanel(new GridLayout(0, 2));
      JPanel contents4 = new JPanel(new GridLayout(0, 2));
      JPanel contents5 = new JPanel(new GridLayout(0, 2));
      contents1.add(contents2, BorderLayout.SOUTH);
      contents1.add(contents3, BorderLayout.SOUTH);
      contents1.add(contents4, BorderLayout.SOUTH);
      contents1.add(contents5, BorderLayout.SOUTH);
      contents2.add(new JLabel("Welcome " + DriverInfo.getName(driverID)));
      holidayStatus = new JLabel("Holidays taken: " +
	  DriverInfo.getHolidaysTaken(driverID) + "     Remain days: " +
	  (25-DriverInfo.getHolidaysTaken(driverID)));
      contents2.add(holidayStatus);
      contents3.add(new JLabel("Start Date of holiday:  dd/mm/yyyy"));
      contents3.add(startDate);
      contents4.add(new JLabel("End Date of holiday:    dd/mm/yyyy"));
      contents4.add(endDate);
      contents5.add(reqHoliday);      reqHoliday.addActionListener(this);
      contents5.add(reNew); reNew.addActionListener(this);
      statusReq = new JLabel("status");
      contents1.add(statusReq);
      // exit the program in case the window it's closed
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      pack();
   }//cont 




  // 
   public void actionPerformed(ActionEvent event)
   {  //for id login
      if (event.getSource() == computeJButton)
        if(Controller.checkDriverID(number1JTextField.getText()))
        { dispose();
          x = new theInterface(DriverInfo.findDriver(number1JTextField.getText()));
          x.setVisible(true);
        }
        else loginLabel.setText("Invalid ID");

      //for driver request
      if(event.getSource()==reqHoliday)
      { //check if NULL
        String x = startDate.getText(),y = endDate.getText();
        if(x.equals("")||y.equals(""))
	{
	  statusReq.setText("Start date and end date can not be empty!");
	  return;
	}
	
	DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        Date test_date1 = null;
	Date test_date2 = null;
      	try
	{
	  test_date1 = df.parse(x);
          test_date2 = df.parse(y);
	}
	catch(ParseException error)
        {
	  statusReq.setText("The format of Start date or end date is wrong!");
	  return;
	}
	if (!df.format(test_date1).equals(x))
	{
	  statusReq.setText("Start date is invalid day!");
	  return;
	}
	else if (!df.format(test_date2).equals(y))
	{
	  statusReq.setText("End date is invalid day!");
	  return;
	}

 	String[] startWords=startDate.getText().split("/");
	String[] endWords = endDate.getText().split("/");
        

	if(startWords.length<=3&&endWords.length<=3)
        {
 		try {
		  int day = Integer.parseInt(startWords[0]);int month = Integer.parseInt(startWords[1])-1;int year = Integer.parseInt(startWords[2]);
		  Date start = new GregorianCalendar(year,month,day).getTime();
     		  Date end = new GregorianCalendar(Integer.parseInt(endWords[2]), Integer.parseInt(endWords[1])-1, Integer.parseInt(endWords[0])).getTime();
      		  //check constraints usign methods in the Controller class	
        	  statusReq.setText(Controller.checkReq(start,end,driverId));
		  holidayStatus.setText("Holidays taken: " + 
		      DriverInfo.getHolidaysTaken(driverId) + "     Remain days: " +
		      (25-DriverInfo.getHolidaysTaken(driverId)));
 		}//try
                catch(NumberFormatException error)
                { 
 		   statusReq.setText("holidays rejected due to invalid dates");
                   return ;   
                }
        }//if	
      }//if reqHoliday
      if(event.getSource()==reNew)
      {    
            log =new  theInterface();
            log.setVisible(true);
            dispose();
      }
   } // actionPerformed

   // create a new cont and display it
   public static void main(String[] args)
   {
      database.openBusDatabase();
      log = new theInterface();
      log.setVisible(true);
   } // main
} // class cont
